﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Notification;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace NotificationPusher.PhoneApp
{
    public class NotificationHelper
    {
        public static void LoadNotificationChannel(Action<string> callback)
        {
            HttpNotificationChannel channel =
                HttpNotificationChannel.Find("NotificationPusherChannel");

            if (channel != null)
            {
                if (channel.ChannelUri != null)
                {
                    Debug.WriteLine(channel.ChannelUri.ToString());
                    callback.Invoke(channel.ChannelUri.ToString());

                    SubscriptToEvents(callback, channel);
                    Connect(channel);
                }
            }
            else
            {
                channel = new HttpNotificationChannel("NotificationPusherChannel");

                SubscriptToEvents(callback, channel);
                Connect(channel);
            }
        }

        private static void Connect(HttpNotificationChannel channel)
        {
            try
            {
                channel.Open();
            }
            catch
            {
                // JUST EAT IT
            }

            if(channel.IsShellToastBound == false)
                channel.BindToShellToast();

            if (channel.IsShellTileBound == false)
                channel.BindToShellTile();
        }

        private static void SubscriptToEvents(Action<string> callback, HttpNotificationChannel channel)
        {
            channel.ChannelUriUpdated += (se, ea) =>
            {
                Debug.WriteLine(ea.ChannelUri);
                callback.Invoke(ea.ChannelUri.ToString());
            };

            channel.HttpNotificationReceived += (se, ea) =>
            {
                if (ea.Notification.Body != null)
                {
                    using (System.IO.StreamReader reader = 
                        new System.IO.StreamReader(ea.Notification.Body))
                    {
                        string message = reader.ReadToEnd();
                        callback.Invoke(message);
                        Debug.WriteLine(message);
                    }
                }  
            };

            channel.ShellToastNotificationReceived += (se, ea) =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    KeyValuePair<string, string> key = ea.Collection.FirstOrDefault();
                    Debug.WriteLine(key.Key);
                    Debug.WriteLine(key.Value);
                });
            };
        }
    }
}
